package cn.edu.hit.core.transfer;

import cn.edu.hit.kg.ScriptContext;
import jdk.nashorn.api.scripting.JSObject;
import org.apache.commons.lang3.StringUtils;
import cn.edu.hit.core.Document;

import java.util.*;

import static cn.edu.hit.core.combine.CombineAdapter.TRANSFER_ARG;
import static cn.edu.hit.core.combine.CombineAdapter.TRANSFER_METHOD;

public class JavaScriptTransfer implements Transfer<List, Object, Document> {

    static ScriptContext scriptContext;

    static {
        scriptContext = ScriptContext.getInstance();
    }

    @Override
    public Object transfer(List source, Document configure) {
        String transferMethod = configure.getString(TRANSFER_METHOD);
        String transferArg = configure.getString(TRANSFER_ARG);

        JSObject combineExpression = scriptContext.getService("combineExpression");
        return combineExpression.call(null, StringUtils.substring(transferMethod, 3), transferArg, configure, source);
    }

    @Override
    public boolean accept(String name) {
        return StringUtils.startsWithIgnoreCase(name, "js:");
    }
}
